package com.wsz.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsz.logistic.bean.Code;
import com.wsz.logistic.bean.Deliverier;
import com.wsz.logistic.bean.OrderStatus;
import com.wsz.logistic.bean.Userinfo;
import com.wsz.logistic.common.exception.RTException;
import com.wsz.logistic.common.form.LoginForm;
import com.wsz.logistic.common.form.RegisterForm;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.mapper.DeliverierMapper;
import com.wsz.logistic.mapper.OrderStatusMapper;
import com.wsz.logistic.mapper.UserinfoMapper;
import com.wsz.logistic.service.CodeService;
import com.wsz.logistic.service.UserinfoService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private CodeService codeService;
    @Autowired
    private DeliverierMapper deliverierMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Override
    public Userinfo login(LoginForm loginForm) {
        Code code = new Code();
        BeanUtils.copyProperties(loginForm, code);
        if (codeService.checkCode(code)) {
            Userinfo userinfo = new Userinfo();
            BeanUtils.copyProperties(loginForm, userinfo);
            userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            return userinfoMapper.login(userinfo);
        }
        return null;
    }

    @Override
    public void register(RegisterForm registerForm) {
        /*添加时先验证是否重名*/
        Userinfo userinfo = new Userinfo();
        BeanUtils.copyProperties(registerForm, userinfo);
        Userinfo user = new Userinfo();
        user.setUsername(userinfo.getUsername());
        user.setRole(userinfo.getRole());
        if (!userinfoMapper.getAll(user).isEmpty()) {
            throw new RTException("用户名已注册！");
        }
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        userinfoMapper.insertSelective(userinfo);
        if (user.getRole() == 3) {
            Deliverier deliverier = new Deliverier();
            //获取新插入的userinfo
            Userinfo newUser = userinfoMapper.login(userinfo);
            //为配送员设置属性
            deliverier.setDeliverierId(newUser.getUserinfoId());
            deliverierMapper.insertSelective(deliverier);
        }
    }

    @Override
    public R getPage(int page, int limit, Userinfo userinfo) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public void saveOrUpdate(Userinfo userinfo) {
        if (userinfo.getUserinfoId() == null) {
            /*添加时先验证是否重名*/
            Userinfo user = new Userinfo();
            user.setUsername(userinfo.getUsername());
            if (userinfoMapper.getAll(user).isEmpty()) {
                userinfo.setPassword(DigestUtils.md5Hex("123456"));
                userinfo.setRole(1);
                userinfoMapper.insertSelective(userinfo);
            } else {
                throw new RTException("用户名已存在！");
            }
        } else {
            if (StringUtils.isNotEmpty(userinfo.getPassword())) {
                userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
            }
            userinfoMapper.updateByPrimaryKeySelective(userinfo);
        }
    }

    public void deleteById(int userinfoId) {
        userinfoMapper.deleteByPrimaryKey(userinfoId);
        /*commentMapper.deleteByUserinfoId(userinfoId);
        articleLikeMapper.deleteByOptId(userinfoId);
        articleLikeMapper.deleteFollowByUserinfoId(userinfoId);*/
    }

    public Userinfo getById(int userinfoId) {
        return userinfoMapper.selectByPrimaryKey(userinfoId);
    }

    public R checkPassword(Userinfo userinfo) {
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        return list.isEmpty() ? R.error("原密码不正确") : R.ok();
    }

    @Override
    public Deliverier getDById(int deliverierId) {
        return deliverierMapper.selectByPrimaryKey(deliverierId);
    }

    @Override
    public void setDeliverier(Deliverier deliverier) {
        deliverierMapper.updateByPrimaryKeySelective(deliverier);
    }

    @Override
    public void setOrderStatus(OrderStatus orderStatus) {
        if (orderStatus.getOrderId() == null) {
            orderStatusMapper.insertSelective(orderStatus);
        } else {
            orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
        }

    }
}
